﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NOSDC.Pocos;
using NOSDC.Entities;
using System.Data.Objects;

namespace NOSDC.Repositories
{
    public class SponsorRepository
    {
        private readonly NOSDCContext _context;

        public SponsorRepository()
        {
            _context = new NOSDCContext();
        }

        public int AddSponsor(Sponsor sponsor)
        {
            _context.Sponsor.AddObject(sponsor);
            return _context.SaveChanges();
        }

        public IEnumerable<Sponsor> GetAllSponsors(){
            return _context.Sponsor.ToList();
        }

        public Sponsor GetSponsorByID(int id)
        {
            Sponsor sp = null;
            using (var context = new NOSDCContext())
            {
                sp = context.Sponsor.Where(s => s.SponsorId == id).FirstOrDefault();
            }

            return sp;
        }

        public int UpdateSponsor(Sponsor s)
        {
            _context.Sponsor.Attach(s);
            var speakerEntry = _context.ObjectStateManager.GetObjectStateEntry(s);
            speakerEntry.SetModified();
            _context.ObjectStateManager.ChangeObjectState(s, System.Data.EntityState.Modified);
            return _context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
        }
    }
}
